﻿//-----------------------------------------------------------------------
// <copyright file="AjaxSample.aspx.cs" company="CMSN Software">
//    Copyright © 2012  CMSN Software
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see http://www.gnu.org/licenses.
// </copyright>
//-----------------------------------------------------------------------
namespace CMSNSoftwareTutorials.SeoFriendlyAjaxApplications
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// generates same output of the ajax
    /// </summary>
    public partial class AjaxSample : System.Web.UI.Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            string commad = Request.Params["_escaped_fragment_"];
            CarDetails cd = new CarDetails();
            switch (commad)
            {
                case "allcars":
                    CarDetails.Text = "<div class='car-details'>" + GetAllCarDetails(cd.AllCars()) + "</div>";
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Gets all car details.
        /// </summary>
        /// <param name="carList">The car list.</param>
        /// <returns>html output for all car details</returns>
        private static string GetAllCarDetails(IList<Car> carList)
        {
            StringBuilder carDetails = new StringBuilder();
            foreach (Car itm in carList)
            {
                carDetails.Append("<div class='car-information'>");
                carDetails.Append("<div>" + itm.Make + "</div>");
                carDetails.Append("<div>" + itm.Model + "</div>");
                carDetails.Append("<div>" + itm.Year + "</div>");
                carDetails.Append("<div>" + itm.Doors + "</div>");
                carDetails.Append("<div>" + itm.Color + "</div>");
                carDetails.Append("<div>" + itm.Price + "</div>");
                carDetails.Append("</div>");
            }

            return carDetails.ToString();
        }
    }
}